
package handling.world.guild;


import client.MapleCharacter;
import client.MapleCharacterUtil;
import client.MapleClient;
import client.inventory.Item;
import client.skills.SkillFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import database.DatabaseConnection;
import handling.world.WorldAllianceService;
import handling.world.WorldBroadcastService;
import handling.world.WorldGuildService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.MapleStatEffect;
import tools.MaplePacketCreator;
import tools.data.output.MaplePacketLittleEndianWriter;
import tools.packet.GuildPacket;
import tools.packet.PacketHelper;
import tools.packet.UIPacket;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;


































 public class MapleGuild implements Serializable {
    /*   36 */   private static final Logger log = LogManager.getLogger(MapleGuild.class);
    public static final long serialVersionUID = 6322150443228168192L;
    private final List<MapleGuildCharacter> members = new CopyOnWriteArrayList<>(); //家族成员的信息
    private final List<MapleGuildCharacter> applyMembers = new ArrayList<>(); //等待加入的家族成员信息
    private final Map<Integer, MapleGuildSkill> guildSkills = new HashMap<>();
    private final String rankTitles[] = new String[5]; // 1 = master, 2 = jr, 5 = lowest member
    private final Map<Integer, MapleBBSThread> bbs = new HashMap<>();
    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    private final int[] guildExp = {0, 15000, 60000, 135000, 240000, 375000, 540000, 735000, 960000, 1215000, 1500000, 1815000, 2160000, 2535000, 2940000, 3375000, 3840000, 4335000, 4860000, 5415000, 6000000, 6615000, 7260000, 7935000, 8640000};

       private String name;
       private String notice;
       private int id;
       private int gp;
       private int contribution;
       private int logo;



    public MapleGuild(int guildid) {
        /*   51 */
        this(guildid, null);

    }










       private int logoColor;








       private int leader;








       private int capacity;








       private int logoBG;








       private int logoBGColor;








       private int signature;








       private int level;








       private boolean bDirty;








       private boolean proper;








       private int allianceid;








       private int invitedid;








       private boolean init;








       private boolean changed;







       private boolean changed_skills;










    public MapleGuild(int guildid, Map<Integer, Map<Integer, MapleBBSReply>> replies) {
        super();
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            PreparedStatement ps = con.prepareStatement("SELECT * FROM guilds WHERE guildid = ?");
            ps.setInt(1, guildid);
            ResultSet rs = ps.executeQuery();
            if (!rs.first()) {
                rs.close();
                ps.close();
                id = -1;
                return;
            }
            id = guildid;
            name = rs.getString("name");
            gp = rs.getInt("GP");
            logo = rs.getInt("logo");
            logoColor = rs.getInt("logoColor");
            logoBG = rs.getInt("logoBG");
            logoBGColor = rs.getInt("logoBGColor");
            capacity = rs.getInt("capacity");
            rankTitles[0] = rs.getString("rank1title");
            rankTitles[1] = rs.getString("rank2title");
            rankTitles[2] = rs.getString("rank3title");
            rankTitles[3] = rs.getString("rank4title");
            rankTitles[4] = rs.getString("rank5title");
            leader = rs.getInt("leader");
            notice = rs.getString("notice");
            signature = rs.getInt("signature");
            allianceid = rs.getInt("alliance");
            rs.close();
            ps.close();

            MapleGuildAlliance alliance = WorldAllianceService.getInstance().getAlliance(allianceid);
            if (alliance == null) {
                allianceid = 0;
            }

            ps = con.prepareStatement("SELECT id, name, level, job, guildrank, guildContribution, alliancerank FROM characters WHERE guildid = ? ORDER BY guildrank ASC, name ASC", ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, guildid);
            rs = ps.executeQuery();
            if (!rs.first()) {
                System.err.println("家族ID: " + id + " 没用成员，系统自动解散该家族。");
                rs.close();
                ps.close();
                writeToDB(true);
                proper = false;
                return;
            }
            boolean leaderCheck = false;
            byte gFix = 0, aFix = 0;
            do {
                int chrId = rs.getInt("id");
                byte gRank = rs.getByte("guildrank"), aRank = rs.getByte("alliancerank");

                if (chrId == leader) {
                    leaderCheck = true;
                    if (gRank != 1) { //needs updating to 1
                        gRank = 1;
                        gFix = 1;
                    }
                    if (alliance != null) {
                        if (alliance.getLeaderId() == chrId && aRank != 1) {
                            aRank = 1;
                            aFix = 1;
                        } else if (alliance.getLeaderId() != chrId && aRank != 2) {
                            aRank = 2;
                            aFix = 2;
                        }
                    }
                } else {
                    if (gRank == 1) {
                        gRank = 2;
                        gFix = 2;
                    }
                    if (aRank < 3) {
                        aRank = 3;
                        aFix = 3;
                    }
                }
                members.add(new MapleGuildCharacter(chrId, rs.getShort("level"), rs.getString("name"), (byte) -1, rs.getInt("job"), gRank, rs.getInt("guildContribution"), aRank, guildid, false));
            } while (rs.next());
            rs.close();
            ps.close();

            if (!leaderCheck) {
                System.err.println("族长[ " + leader + " ]没有在家族ID为 " + id + " 的家族中，系统自动解散这个家族。");
                writeToDB(true);
                proper = false;
                return;
            }

            if (gFix > 0) {
                ps = con.prepareStatement("UPDATE characters SET guildrank = ? WHERE id = ?");
                ps.setByte(1, gFix);
                ps.setInt(2, leader);
                ps.executeUpdate();
                ps.close();
            }

            if (aFix > 0) {
                ps = con.prepareStatement("UPDATE characters SET alliancerank = ? WHERE id = ?");
                ps.setByte(1, aFix);
                ps.setInt(2, leader);
                ps.executeUpdate();
                ps.close();
            }

            ps = con.prepareStatement("SELECT * FROM bbs_threads WHERE guildid = ? ORDER BY localthreadid DESC");
            ps.setInt(1, guildid);
            rs = ps.executeQuery();
            while (rs.next()) {
                int tID = rs.getInt("localthreadid");
                MapleBBSThread thread = new MapleBBSThread(tID, rs.getString("name"), rs.getString("startpost"), rs.getLong("timestamp"), guildid, rs.getInt("postercid"), rs.getInt("icon"));
                if (replies != null && replies.containsKey(rs.getInt("threadid"))) {
                    thread.replies.putAll(replies.get(rs.getInt("threadid")));
                }
                bbs.put(tID, thread);
            }
            rs.close();
            ps.close();

            ps = con.prepareStatement("SELECT * FROM guildskills WHERE guildid = ?");
            ps.setInt(1, guildid);
            rs = ps.executeQuery();
            while (rs.next()) {
                int skillId = rs.getInt("skillid");
                if (skillId < 91000000) { //hack
                    rs.close();
                    ps.close();
                    System.err.println("非家族技能ID: " + skillId + " 在家族ID为 " + id + " 的家族中，系统自动解散该家族。");
                    writeToDB(true);
                    proper = false;
                    return;
                }
                guildSkills.put(skillId, new MapleGuildSkill(skillId, rs.getInt("level"), rs.getLong("timestamp"), rs.getString("purchaser"), "")); //activators not saved
            }
            rs.close();
            ps.close();
            level = calculateLevel();
        } catch (SQLException se) {
            log.error("[MapleGuild] 从数据库中加载家族信息出错." + se);
        }
    }










    public static void loadAll() {
        /*  199 */
        Map<Integer, Map<Integer, MapleBBSReply>> replies = new LinkedHashMap<>();
        /*  200 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  201 */
            PreparedStatement ps = con.prepareStatement("SELECT * FROM bbs_replies");
            /*  202 */
            ResultSet rs = ps.executeQuery();
            /*  203 */
            while (rs.next()) {
                /*  204 */
                int tID = rs.getInt("threadid");
                /*  205 */
                Map<Integer, MapleBBSReply> reply = replies.computeIfAbsent(Integer.valueOf(tID), k -> new HashMap<>());
                /*  206 */
                reply.put(Integer.valueOf(reply.size()), new MapleBBSReply(reply.size(), rs.getInt("postercid"), rs.getString("content"), rs.getLong("timestamp")));

            }
            /*  208 */
            rs.close();
            /*  209 */
            ps.close();
            /*  210 */
            ps = con.prepareStatement("SELECT guildid FROM guilds");
            /*  211 */
            rs = ps.executeQuery();
            /*  212 */
            while (rs.next()) {
                /*  213 */
                WorldGuildService.getInstance().addLoadedGuild(new MapleGuild(rs.getInt("guildid"), replies));

            }
            /*  215 */
            rs.close();
            /*  216 */
            ps.close();
            /*  217 */
        } catch (SQLException se) {
            /*  218 */
            log.error("[MapleGuild] 从数据库中加载家族信息出错." + se);

        }

    }



    public static void loadAll(Object toNotify) {
        /*  223 */
        Map<Integer, Map<Integer, MapleBBSReply>> replies = new LinkedHashMap<>();
        /*  224 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  225 */
            PreparedStatement ps = con.prepareStatement("SELECT * FROM bbs_replies");
            /*  226 */
            ResultSet rs = ps.executeQuery();
            /*  227 */
            while (rs.next()) {
                /*  228 */
                int tID = rs.getInt("threadid");
                /*  229 */
                Map<Integer, MapleBBSReply> reply = replies.computeIfAbsent(Integer.valueOf(tID), k -> new HashMap<>());
                /*  230 */
                reply.put(Integer.valueOf(reply.size()), new MapleBBSReply(reply.size(), rs.getInt("postercid"), rs.getString("content"), rs.getLong("timestamp")));

            }
            /*  232 */
            rs.close();
            /*  233 */
            ps.close();
            /*  234 */
            boolean cont = false;
            /*  235 */
            ps = con.prepareStatement("SELECT guildid FROM guilds");
            /*  236 */
            rs = ps.executeQuery();
            /*  237 */
            while (rs.next()) {
                /*  238 */
                GuildLoad.QueueGuildForLoad(rs.getInt("guildid"), replies);
                /*  239 */
                cont = true;

            }
            /*  241 */
            rs.close();
            /*  242 */
            ps.close();
            /*  243 */
            if (!cont) {

                return;

            }
            /*  246 */
        } catch (SQLException se) {
            /*  247 */
            log.error("[MapleGuild] 从数据库中加载家族信息出错." + se);

        }
        /*  249 */
        AtomicInteger FinishedThreads = new AtomicInteger(0);
        /*  250 */
        GuildLoad.Execute(toNotify);
        /*  251 */
        synchronized (toNotify) {

            try {
                /*  253 */
                toNotify.wait();
                /*  254 */
            } catch (InterruptedException ex) {
                /*  255 */
                ex.printStackTrace();

            }

        }
        /*  258 */
        while (FinishedThreads.incrementAndGet() != 6) {
            /*  259 */
            synchronized (toNotify) {

                try {
                    /*  261 */
                    toNotify.wait();
                    /*  262 */
                } catch (InterruptedException ex) {
                    /*  263 */
                    ex.printStackTrace();

                }

            }

        }

    }






    public static int createGuild(int leaderId, String name) {
        /*  273 */
        if (name.length() > 12) {
            /*  274 */
            return 0;

        }
        /*  276 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  277 */
            PreparedStatement ps = con.prepareStatement("SELECT guildid FROM guilds WHERE name = ?");
            /*  278 */
            ps.setString(1, name);
            /*  279 */
            ResultSet rs = ps.executeQuery();

            /*  281 */
            if (rs.first()) {
                /*  282 */
                rs.close();
                /*  283 */
                ps.close();
                /*  284 */
                return 0;

            }
            /*  286 */
            ps.close();
            /*  287 */
            rs.close();

            /*  289 */
            ps = con.prepareStatement("INSERT INTO guilds (`leader`, `name`, `signature`, `alliance`) VALUES (?, ?, ?, 0)", 1);
            /*  290 */
            ps.setInt(1, leaderId);
            /*  291 */
            ps.setString(2, name);
            /*  292 */
            ps.setInt(3, (int) (System.currentTimeMillis() / 1000L));
            /*  293 */
            ps.executeUpdate();
            /*  294 */
            rs = ps.getGeneratedKeys();
            /*  295 */
            int ret = 0;
            /*  296 */
            if (rs.next()) {
                /*  297 */
                ret = rs.getInt(1);

            }
            /*  299 */
            rs.close();
            /*  300 */
            ps.close();
            /*  301 */
            return ret;
            /*  302 */
        } catch (SQLException se) {
            /*  303 */
            log.error("[MapleGuild] 创建家族信息出错." + se);
            /*  304 */
            return 0;

        }

    }






    public static MapleGuildResponse sendInvite(MapleClient c, String targetName) {
        /*  312 */
        MapleCharacter target = c.getChannelServer().getPlayerStorage().getCharacterByName(targetName);
        /*  313 */
        if (target == null) {
            /*  314 */
            return MapleGuildResponse.找不到角色;

        }
        /*  316 */
        if (target.getGuildId() > 0) {
            /*  317 */
            return MapleGuildResponse.已经有家族;

        }
        /*  319 */
        if (c.getPlayer().getGuild().getMembers().size() >= c.getPlayer().getGuild().getCapacity()) {
            /*  320 */
            return MapleGuildResponse.家族人数已满;

        }
        /*  322 */
        c.announce(MaplePacketCreator.showRedNotice("已邀请'" + targetName + "'加入公会。"));
        /*  323 */
        target.getClient().announce(GuildPacket.guildInvite(c.getPlayer().getGuildId(), c.getPlayer().getGuild().getName(), c.getPlayer().getName(), c.getPlayer().getLevel(), c.getPlayer().getJob()));
        /*  324 */
        return null;

    }



    public static void setOfflineGuildStatus(int guildId, byte guildrank, int contribution, byte alliancerank, int chrId) {
        /*  328 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  329 */
            PreparedStatement ps = con.prepareStatement("UPDATE characters SET guildid = ?, guildrank = ?, guildContribution = ?, alliancerank = ? WHERE id = ?");
            /*  330 */
            ps.setInt(1, guildId);
            /*  331 */
            ps.setInt(2, guildrank);
            /*  332 */
            ps.setInt(3, contribution);
            /*  333 */
            ps.setInt(4, alliancerank);
            /*  334 */
            ps.setInt(5, chrId);
            /*  335 */
            ps.executeUpdate();
            /*  336 */
            ps.close();
            /*  337 */
        } catch (SQLException se) {
            /*  338 */
            System.out.println("SQLException: " + se.getLocalizedMessage());

        }

    }



    public boolean isProper() {
        /*  343 */
        return this.proper;

    }



    public final void writeToDB(boolean bDisband) {
        /*  347 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  348 */
            if (!bDisband) {
                /*  349 */
                StringBuilder buf = new StringBuilder("UPDATE guilds SET GP = ?, logo = ?, logoColor = ?, logoBG = ?, logoBGColor = ?, ");
                /*  350 */
                for (int i = 1; i < 6; i++) {
                    /*  351 */
                    buf.append("rank").append(i).append("title = ?, ");

                }
                /*  353 */
                buf.append("capacity = ?, notice = ?, alliance = ?, leader = ? WHERE guildid = ?");

                /*  355 */
                PreparedStatement ps = con.prepareStatement(buf.toString());
                /*  356 */
                ps.setInt(1, this.gp);
                /*  357 */
                ps.setInt(2, this.logo);
                /*  358 */
                ps.setInt(3, this.logoColor);
                /*  359 */
                ps.setInt(4, this.logoBG);
                /*  360 */
                ps.setInt(5, this.logoBGColor);
                /*  361 */
                ps.setString(6, this.rankTitles[0]);
                /*  362 */
                ps.setString(7, this.rankTitles[1]);
                /*  363 */
                ps.setString(8, this.rankTitles[2]);
                /*  364 */
                ps.setString(9, this.rankTitles[3]);
                /*  365 */
                ps.setString(10, this.rankTitles[4]);
                /*  366 */
                ps.setInt(11, this.capacity);
                /*  367 */
                ps.setString(12, this.notice);
                /*  368 */
                ps.setInt(13, this.allianceid);
                /*  369 */
                ps.setInt(14, this.leader);
                /*  370 */
                ps.setInt(15, this.id);
                /*  371 */
                ps.executeUpdate();
                /*  372 */
                ps.close();

                /*  374 */
                if (this.changed) {
                    /*  375 */
                    ps = con.prepareStatement("DELETE FROM bbs_threads WHERE guildid = ?");
                    /*  376 */
                    ps.setInt(1, this.id);
                    /*  377 */
                    ps.execute();
                    /*  378 */
                    ps.close();

                    /*  380 */
                    ps = con.prepareStatement("DELETE FROM bbs_replies WHERE guildid = ?");
                    /*  381 */
                    ps.setInt(1, this.id);
                    /*  382 */
                    ps.execute();
                    /*  383 */
                    ps.close();

                    /*  385 */
                    PreparedStatement pse = con.prepareStatement("INSERT INTO bbs_replies (`threadid`, `postercid`, `timestamp`, `content`, `guildid`) VALUES (?, ?, ?, ?, ?)");
                    /*  386 */
                    ps = con.prepareStatement("INSERT INTO bbs_threads(`postercid`, `name`, `timestamp`, `icon`, `startpost`, `guildid`, `localthreadid`) VALUES(?, ?, ?, ?, ?, ?, ?)", 1);
                    /*  387 */
                    ps.setInt(6, this.id);
                    /*  388 */
                    for (MapleBBSThread bb : this.bbs.values()) {
                        /*  389 */
                        ps.setInt(1, bb.ownerID);
                        /*  390 */
                        ps.setString(2, bb.name);
                        /*  391 */
                        ps.setLong(3, bb.timestamp);
                        /*  392 */
                        ps.setInt(4, bb.icon);
                        /*  393 */
                        ps.setString(5, bb.text);
                        /*  394 */
                        ps.setInt(7, bb.localthreadID);
                        /*  395 */
                        ps.execute();
                        /*  396 */
                        ResultSet rs = ps.getGeneratedKeys();
                        /*  397 */
                        if (!rs.next()) {
                            /*  398 */
                            rs.close();

                            continue;

                        }
                        /*  401 */
                        int ourId = rs.getInt(1);
                        /*  402 */
                        rs.close();
                        /*  403 */
                        pse.setInt(5, this.id);
                        /*  404 */
                        for (MapleBBSReply r : bb.replies.values()) {
                            /*  405 */
                            pse.setInt(1, ourId);
                            /*  406 */
                            pse.setInt(2, r.ownerID);
                            /*  407 */
                            pse.setLong(3, r.timestamp);
                            /*  408 */
                            pse.setString(4, r.content);
                            /*  409 */
                            pse.addBatch();

                        }

                    }
                    /*  412 */
                    pse.executeBatch();
                    /*  413 */
                    pse.close();
                    /*  414 */
                    ps.close();

                }
                /*  416 */
                if (this.changed_skills) {
                    /*  417 */
                    ps = con.prepareStatement("DELETE FROM guildskills WHERE guildid = ?");
                    /*  418 */
                    ps.setInt(1, this.id);
                    /*  419 */
                    ps.execute();
                    /*  420 */
                    ps.close();

                    /*  422 */
                    ps = con.prepareStatement("INSERT INTO guildskills(`guildid`, `skillid`, `level`, `timestamp`, `purchaser`) VALUES(?, ?, ?, ?, ?)");
                    /*  423 */
                    ps.setInt(1, this.id);
                    /*  424 */
                    for (MapleGuildSkill mapleGuildSkill : this.guildSkills.values()) {
                        /*  425 */
                        ps.setInt(2, mapleGuildSkill.skillID);
                        /*  426 */
                        ps.setByte(3, (byte) mapleGuildSkill.level);
                        /*  427 */
                        ps.setLong(4, mapleGuildSkill.timestamp);
                        /*  428 */
                        ps.setString(5, mapleGuildSkill.purchaser);
                        /*  429 */
                        ps.execute();

                    }
                    /*  431 */
                    ps.close();

                }
                /*  433 */
                this.changed_skills = false;
                /*  434 */
                this.changed = false;

            } else {
                /*  436 */
                PreparedStatement ps = con.prepareStatement("DELETE FROM bbs_threads WHERE guildid = ?");
                /*  437 */
                ps.setInt(1, this.id);
                /*  438 */
                ps.execute();
                /*  439 */
                ps.close();


                /*  442 */
                ps = con.prepareStatement("DELETE FROM bbs_replies WHERE guildid = ?");
                /*  443 */
                ps.setInt(1, this.id);
                /*  444 */
                ps.execute();
                /*  445 */
                ps.close();

                /*  447 */
                ps = con.prepareStatement("DELETE FROM guildskills WHERE guildid = ?");
                /*  448 */
                ps.setInt(1, this.id);
                /*  449 */
                ps.execute();
                /*  450 */
                ps.close();

                /*  452 */
                ps = con.prepareStatement("DELETE FROM guilds WHERE guildid = ?");
                /*  453 */
                ps.setInt(1, this.id);
                /*  454 */
                ps.executeUpdate();
                /*  455 */
                ps.close();


                /*  458 */
                if (this.allianceid > 0) {
                    /*  459 */
                    MapleGuildAlliance alliance = WorldAllianceService.getInstance().getAlliance(this.allianceid);
                    /*  460 */
                    if (alliance != null) {
                        /*  461 */
                        alliance.removeGuild(this.id, false);

                    }

                }

                /*  465 */
                broadcast(GuildPacket.guildDisband(this.id));

            }
            /*  467 */
        } catch (SQLException se) {
            /*  468 */
            log.error("[MapleGuild] 保存家族信息出错." + se);

        }

    }



    public int getId() {
        /*  473 */
        return this.id;

    }



    public int getLeaderId() {
        /*  477 */
        return this.leader;

    }



    public MapleCharacter getLeader(MapleClient c) {
        /*  481 */
        return c.getChannelServer().getPlayerStorage().getCharacterById(this.leader);

    }






    public int getGP() {
        /*  488 */
        return this.gp;

    }






    public int getGontribution() {
        /*  495 */
        return this.contribution;

    }



    public int getLogo() {
        /*  499 */
        return this.logo;

    }



    public void setLogo(int l) {
        /*  503 */
        this.logo = l;

    }



    public int getLogoColor() {
        /*  507 */
        return this.logoColor;

    }



    public void setLogoColor(int c) {
        /*  511 */
        this.logoColor = c;

    }



    public int getLogoBG() {
        /*  515 */
        return this.logoBG;

    }



    public void setLogoBG(int bg) {
        /*  519 */
        this.logoBG = bg;

    }



    public int getLogoBGColor() {
        /*  523 */
        return this.logoBGColor;

    }



    public void setLogoBGColor(int c) {
        /*  527 */
        this.logoBGColor = c;

    }



    public String getNotice() {
        /*  531 */
        if (this.notice == null) {
            /*  532 */
            return "";

        }
        /*  534 */
        return this.notice;

    }



    public String getName() {
        /*  538 */
        return this.name;

    }



    public int getCapacity() {
        /*  542 */
        return this.capacity;

    }



    public int getSignature() {
        /*  546 */
        return this.signature;

    }



    public void broadcast(byte[] packet) {
        /*  550 */
        broadcast(packet, -1, BCOp.NONE);

    }



    public void broadcast(byte[] packet, int exception) {
        /*  554 */
        broadcast(packet, exception, BCOp.NONE);

    }



    public void broadcast(byte[] packet, int exceptionId, BCOp bcop) {
        /*  558 */
        this.lock.writeLock().lock();

        try {
            /*  560 */
            buildNotifications();

        } finally {
            /*  562 */
            this.lock.writeLock().unlock();

        }

        /*  565 */
        this.lock.readLock().lock();

        try {
            /*  567 */
            for (MapleGuildCharacter mgc : this.members) {
                /*  568 */
                if (bcop == BCOp.DISBAND) {
                    /*  569 */
                    if (mgc.isOnline()) {
                        /*  570 */
                        WorldGuildService.getInstance().setGuildAndRank(mgc.getId(), 0, 5, 0, 5);
                        continue;

                    }
                    /*  572 */
                    setOfflineGuildStatus(0, (byte) 5, 0, (byte) 5, mgc.getId());
                    continue;

                }
                /*  574 */
                if ((mgc.isOnline() && mgc.getId() != exceptionId) || (mgc.isOnline() && bcop == BCOp.CHAT)) {
                    /*  575 */
                    if (bcop == BCOp.EMBELMCHANGE) {
                        /*  576 */
                        WorldGuildService.getInstance().changeEmblem(this.id, mgc.getId(), this);
                        continue;

                    }
                    /*  578 */
                    WorldBroadcastService.getInstance().sendGuildPacket(mgc.getId(), packet, exceptionId, this.id, (bcop == BCOp.CHAT));

                }

            }

        } finally {

            /*  583 */
            this.lock.readLock().unlock();

        }

    }



    private void buildNotifications() {
        /*  588 */
        if (!this.bDirty) {

            return;

        }
        /*  591 */
        List<Integer> mem = new LinkedList<>();
        /*  592 */
        for (MapleGuildCharacter mgc : this.members) {
            /*  593 */
            if (!mgc.isOnline()) {

                continue;

            }
            /*  596 */
            if (mem.contains(Integer.valueOf(mgc.getId())) || mgc.getGuildId() != this.id) {
                /*  597 */
                this.members.remove(mgc);

                continue;

            }
            /*  600 */
            mem.add(Integer.valueOf(mgc.getId()));

        }
        /*  602 */
        this.bDirty = false;

    }



    public void setOnline(int chrId, boolean online, int channel) {
        /*  606 */
        boolean isBroadcast = true;
        /*  607 */
        for (MapleGuildCharacter mgc : this.members) {
            /*  608 */
            if (mgc.getGuildId() == this.id && mgc.getId() == chrId) {
                /*  609 */
                if (mgc.isOnline() == online) {
                    /*  610 */
                    isBroadcast = false;

                }
                /*  612 */
                mgc.setOnline(online);
                /*  613 */
                mgc.setChannel((byte) channel);

                break;

            }

        }
        /*  617 */
        if (isBroadcast) {
            /*  618 */
            broadcast(GuildPacket.guildMemberOnline(this.id, chrId, online), chrId);

        }



        /*  623 */
        this.bDirty = true;
        /*  624 */
        this.init = true;

    }










    public void guildChat(String name, int chrId, String msg, Item item) {
        /*  635 */
        broadcast(MaplePacketCreator.multiChat(true, name, msg, 2, item), chrId);

    }






    public void allianceChat(String name, int chrId, String msg) {
        /*  642 */
        broadcast(MaplePacketCreator.multiChat(name, msg, 3), chrId);

    }



    public String getRankTitle(int rank) {
        /*  646 */
        return this.rankTitles[rank - 1];

    }



    public int getAllianceId() {
        /*  650 */
        return this.allianceid;

    }



    public void setAllianceId(int a) {
        /*  654 */
        this.allianceid = a;
        /*  655 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  656 */
            PreparedStatement ps = con.prepareStatement("UPDATE guilds SET alliance = ? WHERE guildid = ?");
            /*  657 */
            ps.setInt(1, a);
            /*  658 */
            ps.setInt(2, this.id);
            /*  659 */
            ps.execute();
            /*  660 */
            ps.close();
            /*  661 */
        } catch (SQLException e) {
            /*  662 */
            log.error("[MapleGuild] 保存家族联盟信息出错." + e);

        }

    }



    public int getInvitedId() {
        /*  667 */
        return this.invitedid;

    }



    public void setInvitedId(int iid) {
        /*  671 */
        this.invitedid = iid;

    }






    public int addGuildMember(MapleGuildCharacter newMember) {
        /*  678 */
        this.lock.writeLock().lock();

        try {
            /*  680 */
            if (this.members.size() >= this.capacity) {
                /*  681 */
                return 0;

            }
            /*  683 */
            for (int i = this.members.size() - 1; i >= 0; i--) {
                /*  684 */
                if (((MapleGuildCharacter) this.members.get(i)).getGuildRank() < 5 || ((MapleGuildCharacter) this.members.get(i)).getName().compareTo(newMember.getName()) < 0) {
                    /*  685 */
                    this.members.add(i + 1, newMember);
                    /*  686 */
                    this.bDirty = true;


                    break;

                }

            }
            /*  691 */
            for (MapleGuildCharacter mgcc : this.applyMembers) {
                /*  692 */
                if (mgcc.getId() == newMember.getId()) {
                    /*  693 */
                    this.applyMembers.remove(mgcc);

                    break;

                }

            }

        } finally {
            /*  698 */
            this.lock.writeLock().unlock();

        }
        /*  700 */
        broadcast(GuildPacket.newGuildMember(newMember, false));
        /*  701 */
        gainGP(500, true, newMember.getId());
        /*  702 */
        if (this.allianceid > 0) {
            /*  703 */
            WorldAllianceService.getInstance().sendGuild(this.allianceid);

        }
        /*  705 */
        return 1;

    }



    public int addGuildMember(int chrId) {
        /*  709 */
        MapleGuildCharacter newMember = getApplyMGC(chrId);
        /*  710 */
        if (newMember == null) {
            /*  711 */
            return 0;

        }
        /*  713 */
        this.applyMembers.remove(newMember);

        /*  715 */
        this.lock.writeLock().lock();

        try {
            /*  717 */
            if (this.members.size() >= this.capacity) {
                /*  718 */
                return 0;

            }
            /*  720 */
            for (int i = this.members.size() - 1; i >= 0; i--) {
                /*  721 */
                if (((MapleGuildCharacter) this.members.get(i)).getGuildRank() < 5 || ((MapleGuildCharacter) this.members.get(i)).getName().compareTo(newMember.getName()) < 0) {
                    /*  722 */
                    this.members.add(i + 1, newMember);
                    /*  723 */
                    this.bDirty = true;

                    break;

                }

            }

        } finally {
            /*  728 */
            this.lock.writeLock().unlock();

        }
        /*  730 */
        gainGP(500, true, newMember.getId());
        /*  731 */
        broadcast(GuildPacket.newGuildMember(newMember, false));
        /*  732 */
        if (this.allianceid > 0) {
            /*  733 */
            WorldAllianceService.getInstance().sendGuild(this.allianceid);

        }
        /*  735 */
        return 1;

    }






    public int addGuildApplyMember(MapleGuildCharacter applyMember) {
        /*  742 */
        if (getApplyMGC(applyMember.getId()) != null) {
            /*  743 */
            return 0;

        }
        /*  745 */
        this.applyMembers.add(applyMember);
        /*  746 */
        broadcast(GuildPacket.newGuildMember(applyMember, true));
        /*  747 */
        return 1;

    }







    public int denyGuildApplyMember(int fromId) {
        /*  755 */
        MapleGuildCharacter applyMember = getApplyMGC(fromId);
        /*  756 */
        if (applyMember == null) {
            /*  757 */
            return 0;

        }
        /*  759 */
        this.applyMembers.remove(applyMember);
        /*  760 */
        return 1;

    }






    public void leaveGuild(MapleGuildCharacter mgc) {
        /*  767 */
        this.lock.writeLock().lock();

        try {
            /*  769 */
            for (MapleGuildCharacter mgcc : this.members) {
                /*  770 */
                if (mgcc.getId() == mgc.getId()) {
                    /*  771 */
                    broadcast(GuildPacket.memberLeft(mgcc, false));
                    /*  772 */
                    this.bDirty = true;
                    /*  773 */
                    gainGP((mgcc.getGuildContribution() > 0) ? -mgcc.getGuildContribution() : -500);
                    /*  774 */
                    this.members.remove(mgcc);
                    /*  775 */
                    if (mgc.isOnline()) {
                        /*  776 */
                        WorldGuildService.getInstance().setGuildAndRank(mgcc.getId(), 0, 5, 0, 5);
                        break;

                    }
                    /*  778 */
                    setOfflineGuildStatus(0, (byte) 5, 0, (byte) 5, mgcc.getId());


                    break;

                }

            }

        } finally {
            /*  784 */
            this.lock.writeLock().unlock();

        }
        /*  786 */
        if (this.bDirty && this.allianceid > 0) {
            /*  787 */
            WorldAllianceService.getInstance().sendGuild(this.allianceid);

        }

    }






    public void expelMember(MapleGuildCharacter initiator, String name, int chrId) {
        /*  795 */
        this.lock.writeLock().lock();

        try {
            /*  797 */
            for (MapleGuildCharacter mgc : this.members) {
                /*  798 */
                if (mgc.getId() == chrId && initiator.getGuildRank() < mgc.getGuildRank()) {
                    /*  799 */
                    broadcast(GuildPacket.memberLeft(mgc, true));
                    /*  800 */
                    this.bDirty = true;
                    /*  801 */
                    gainGP((mgc.getGuildContribution() > 0) ? -mgc.getGuildContribution() : -500);
                    /*  802 */
                    if (mgc.isOnline()) {
                        /*  803 */
                        WorldGuildService.getInstance().setGuildAndRank(chrId, 0, 5, 0, 5);

                    } else {
                        /*  805 */
                        MapleCharacterUtil.sendNote(mgc.getName(), initiator.getName(), "被家族除名了。", 0);
                        /*  806 */
                        setOfflineGuildStatus(0, (byte) 5, 0, (byte) 5, chrId);

                    }
                    /*  808 */
                    this.members.remove(mgc);

                    break;

                }

            }

        } finally {
            /*  813 */
            this.lock.writeLock().unlock();

        }
        /*  815 */
        if (this.bDirty && this.allianceid > 0) {
            /*  816 */
            WorldAllianceService.getInstance().sendGuild(this.allianceid);

        }

    }



    public void changeARank() {
        /*  821 */
        changeARank(false);

    }



    public void changeARank(boolean leader) {
        /*  825 */
        if (this.allianceid <= 0) {

            return;

        }
        /*  828 */
        for (MapleGuildCharacter mgc : this.members) {
            /*  829 */
            byte newRank = 3;
            /*  830 */
            if (this.leader == mgc.getId()) {
                /*  831 */
                newRank = (byte) (leader ? 1 : 2);

            }
            /*  833 */
            if (mgc.isOnline()) {
                /*  834 */
                WorldGuildService.getInstance().setGuildAndRank(mgc.getId(), this.id, mgc.getGuildRank(), mgc.getGuildContribution(), newRank);

            } else {
                /*  836 */
                setOfflineGuildStatus(this.id, mgc.getGuildRank(), mgc.getGuildContribution(), newRank, mgc.getId());

            }
            /*  838 */
            mgc.setAllianceRank(newRank);

        }
        /*  840 */
        WorldAllianceService.getInstance().sendGuild(this.allianceid);

    }



    public void changeARank(int newRank) {
        /*  844 */
        if (this.allianceid <= 0) {

            return;

        }
        /*  847 */
        for (MapleGuildCharacter mgc : this.members) {
            /*  848 */
            if (mgc.isOnline()) {
                /*  849 */
                WorldGuildService.getInstance().setGuildAndRank(mgc.getId(), this.id, mgc.getGuildRank(), mgc.getGuildContribution(), newRank);

            } else {
                /*  851 */
                setOfflineGuildStatus(this.id, mgc.getGuildRank(), mgc.getGuildContribution(), (byte) newRank, mgc.getId());

            }
            /*  853 */
            mgc.setAllianceRank((byte) newRank);

        }
        /*  855 */
        WorldAllianceService.getInstance().sendGuild(this.allianceid);

    }



    public boolean changeARank(int chrId, int newRank) {
        /*  859 */
        if (this.allianceid <= 0) {
            /*  860 */
            return false;

        }
        /*  862 */
        for (MapleGuildCharacter mgc : this.members) {
            /*  863 */
            if (chrId == mgc.getId()) {
                /*  864 */
                if (mgc.isOnline()) {
                    /*  865 */
                    WorldGuildService.getInstance().setGuildAndRank(chrId, this.id, mgc.getGuildRank(), mgc.getGuildContribution(), newRank);

                } else {
                    /*  867 */
                    setOfflineGuildStatus(this.id, mgc.getGuildRank(), mgc.getGuildContribution(), (byte) newRank, chrId);

                }
                /*  869 */
                mgc.setAllianceRank((byte) newRank);
                /*  870 */
                WorldAllianceService.getInstance().sendGuild(this.allianceid);
                /*  871 */
                return true;

            }

        }
        /*  874 */
        return false;

    }






    public void changeGuildLeader(int newLeader) {
        /*  881 */
        if (changeRank(newLeader, 1) && changeRank(this.leader, 2)) {
            /*  882 */
            if (this.allianceid > 0) {
                /*  883 */
                int aRank = getMGC(this.leader).getAllianceRank();
                /*  884 */
                if (aRank == 1) {
                    /*  885 */
                    WorldAllianceService.getInstance().changeAllianceLeader(this.allianceid, newLeader, true);

                } else {
                    /*  887 */
                    changeARank(newLeader, aRank);

                }
                /*  889 */
                changeARank(this.leader, 3);

            }
            /*  891 */
            broadcast(GuildPacket.guildLeaderChanged(this.id, this.leader, newLeader, this.allianceid));
            /*  892 */
            this.leader = newLeader;
            /*  893 */
            try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
                /*  894 */
                PreparedStatement ps = con.prepareStatement("UPDATE guilds SET leader = ? WHERE guildid = ?");
                /*  895 */
                ps.setInt(1, newLeader);
                /*  896 */
                ps.setInt(2, this.id);
                /*  897 */
                ps.execute();
                /*  898 */
                ps.close();
                /*  899 */
            } catch (SQLException e) {
                /*  900 */
                log.error("[MapleGuild] Saving leaderid ERROR." + e);

            }

        }

    }






    public boolean changeRank(int chrId, int newRank) {
        /*  909 */
        for (MapleGuildCharacter mgc : this.members) {
            /*  910 */
            if (chrId == mgc.getId()) {
                /*  911 */
                if (mgc.isOnline()) {
                    /*  912 */
                    WorldGuildService.getInstance().setGuildAndRank(chrId, this.id, newRank, mgc.getGuildContribution(), mgc.getAllianceRank());

                } else {
                    /*  914 */
                    setOfflineGuildStatus(this.id, (byte) newRank, mgc.getGuildContribution(), mgc.getAllianceRank(), chrId);

                }
                /*  916 */
                mgc.setGuildRank((byte) newRank);
                /*  917 */
                broadcast(GuildPacket.changeRank(mgc));
                /*  918 */
                return true;

            }

        }
        /*  921 */
        return false;

    }






    public void setGuildNotice(String notice) {
        /*  928 */
        this.notice = notice;
        /*  929 */
        broadcast(GuildPacket.guildNotice(this.id, notice));

    }



    public void memberLevelJobUpdate(MapleGuildCharacter mgc) {
        /*  933 */
        for (MapleGuildCharacter member : this.members) {
            /*  934 */
            if (member.getId() == mgc.getId()) {
                /*  935 */
                int old_level = member.getLevel();
                /*  936 */
                int old_job = member.getJobId();
                /*  937 */
                member.setJobId(mgc.getJobId());
                /*  938 */
                member.setLevel((short) mgc.getLevel());
                /*  939 */
                if (mgc.getLevel() > old_level) {
                    /*  940 */
                    gainGP((mgc.getLevel() - old_level) * mgc.getLevel(), false, mgc.getId());

                }

                /*  943 */
                if (old_level != mgc.getLevel()) {
                    /*  944 */
                    broadcast(MaplePacketCreator.sendLevelup(false, mgc.getLevel(), mgc.getName()), mgc.getId());

                }
                /*  946 */
                if (old_job != mgc.getJobId()) {
                    /*  947 */
                    broadcast(MaplePacketCreator.sendJobup(false, mgc.getJobId(), mgc.getName()), mgc.getId());

                }
                /*  949 */
                broadcast(GuildPacket.guildMemberLevelJobUpdate(mgc));
                /*  950 */
                if (this.allianceid > 0) {
                    /*  951 */
                    WorldAllianceService.getInstance().sendGuild(GuildPacket.updateAlliance(mgc, this.allianceid), this.id, this.allianceid);

                }

                break;

            }

        }

    }






    public void changeRankTitle(String[] ranks) {
        /*  962 */
        System.arraycopy(ranks, 0, this.rankTitles, 0, 5);
        /*  963 */
        broadcast(GuildPacket.rankTitleChange(this.id, ranks));

    }






    public void disbandGuild() {
        /*  970 */
        writeToDB(true);
        /*  971 */
        broadcast(null, -1, BCOp.DISBAND);

    }






    public void setGuildEmblem(short bg, byte bgcolor, short logo, byte logocolor) {
        /*  978 */
        this.logoBG = bg;
        /*  979 */
        this.logoBGColor = bgcolor;
        /*  980 */
        this.logo = logo;
        /*  981 */
        this.logoColor = logocolor;
        /*  982 */
        broadcast(null, -1, BCOp.EMBELMCHANGE);

        /*  984 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /*  985 */
            PreparedStatement ps = con.prepareStatement("UPDATE guilds SET logo = ?, logoColor = ?, logoBG = ?, logoBGColor = ? WHERE guildid = ?");
            /*  986 */
            ps.setInt(1, logo);
            /*  987 */
            ps.setInt(2, this.logoColor);
            /*  988 */
            ps.setInt(3, this.logoBG);
            /*  989 */
            ps.setInt(4, this.logoBGColor);
            /*  990 */
            ps.setInt(5, this.id);
            /*  991 */
            ps.execute();
            /*  992 */
            ps.close();
            /*  993 */
        } catch (SQLException e) {
            /*  994 */
            log.error("[MapleGuild] Saving guild logo / BG colo ERROR." + e);

        }

    }






    public MapleGuildCharacter getMGC(int chrId) {
        /* 1002 */
        for (MapleGuildCharacter mgc : this.members) {
            /* 1003 */
            if (mgc.getId() == chrId) {
                /* 1004 */
                return mgc;

            }

        }
        /* 1007 */
        return null;

    }






    public MapleGuildCharacter getApplyMGC(int chrId) {
        /* 1014 */
        for (MapleGuildCharacter mgc : this.applyMembers) {
            /* 1015 */
            if (mgc.getId() == chrId) {
                /* 1016 */
                return mgc;

            }

        }
        /* 1019 */
        return null;

    }






    public boolean increaseCapacity(boolean trueMax) {
        /* 1026 */
        if (this.capacity >= (trueMax ? 200 : 100) || this.capacity + 5 > (trueMax ? 200 : 100)) {
            /* 1027 */
            return false;

        }
        /* 1029 */
        if (trueMax && this.gp < 25000) {
            /* 1030 */
            return false;

        }
        /* 1032 */
        if (trueMax && this.gp - 25000 < getGuildExpNeededForLevel(getLevel() - 1)) {
            /* 1033 */
            return false;

        }
        /* 1035 */
        this.capacity += 5;
        /* 1036 */
        broadcast(GuildPacket.guildCapacityChange(this.id, this.capacity));

        /* 1038 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /* 1039 */
            PreparedStatement ps = con.prepareStatement("UPDATE guilds SET capacity = ? WHERE guildid = ?");
            /* 1040 */
            ps.setInt(1, this.capacity);
            /* 1041 */
            ps.setInt(2, this.id);
            /* 1042 */
            ps.execute();
            /* 1043 */
            ps.close();
            /* 1044 */
        } catch (SQLException e) {
            /* 1045 */
            log.error("[MapleGuild] Saving guild capacity ERROR." + e);

        }
        /* 1047 */
        return true;

    }






    public void gainGP(int amount) {
        /* 1054 */
        gainGP(amount, true, -1);

    }



    public void gainGP(int amount, boolean broadcast) {
        /* 1058 */
        gainGP(amount, broadcast, -1);

    }



    public void gainGP(int amount, boolean broadcast, int chrId) {
        /* 1062 */
        if (amount == 0) {

            return;

        }
        /* 1065 */
        if (amount + this.gp < 0) {
            /* 1066 */
            amount = -this.gp;

        }
        /* 1068 */
        this.gp += amount;
        /* 1069 */
        this.level = calculateLevel();
        /* 1070 */
        broadcast(GuildPacket.updateGuildInfo(this.id, this.gp, this.level));
        /* 1071 */
        if (chrId > 0 && amount > 0) {
            /* 1072 */
            MapleGuildCharacter mgc = getMGC(chrId);
            /* 1073 */
            if (mgc != null) {
                /* 1074 */
                mgc.setGuildContribution(mgc.getGuildContribution() + amount);
                /* 1075 */
                if (mgc.isOnline()) {
                    /* 1076 */
                    WorldGuildService.getInstance().setGuildAndRank(chrId, this.id, mgc.getGuildRank(), mgc.getGuildContribution(), mgc.getAllianceRank());

                } else {
                    /* 1078 */
                    setOfflineGuildStatus(this.id, mgc.getGuildRank(), mgc.getGuildContribution(), mgc.getAllianceRank(), chrId);

                }
                /* 1080 */
                broadcast(GuildPacket.updatePlayerContribution(this.id, chrId, mgc.getGuildContribution()));

            }

        }
        /* 1083 */
        if (broadcast) {
            /* 1084 */
            broadcast(UIPacket.getGPMsg(amount));

        }

    }



    public Collection<MapleGuildSkill> getSkills() {
        /* 1089 */
        return this.guildSkills.values();

    }



    public int getSkillLevel(int skillId) {
        /* 1093 */
        if (!this.guildSkills.containsKey(Integer.valueOf(skillId))) {
            /* 1094 */
            return 0;

        }
        /* 1096 */
        return ((MapleGuildSkill) this.guildSkills.get(Integer.valueOf(skillId))).level;

    }






    public boolean activateSkill(int skillId, String name) {
        /* 1103 */
        if (!this.guildSkills.containsKey(Integer.valueOf(skillId))) {
            /* 1104 */
            return false;

        }
        /* 1106 */
        MapleGuildSkill ourSkill = this.guildSkills.get(Integer.valueOf(skillId));
        /* 1107 */
        MapleStatEffect effect = SkillFactory.getSkill(skillId).getEffect(ourSkill.level);
        /* 1108 */
        if (ourSkill.timestamp > System.currentTimeMillis() || effect.getPeriod() <= 0) {
            /* 1109 */
            return false;

        }
        /* 1111 */
        ourSkill.timestamp = System.currentTimeMillis() + effect.getPeriod() * 60000L;
        /* 1112 */
        ourSkill.activator = name;
        /* 1113 */
        broadcast(GuildPacket.guildSkillPurchased(this.id, skillId, ourSkill.level, ourSkill.timestamp, ourSkill.purchaser, name));
        /* 1114 */
        return true;

    }






    public boolean purchaseSkill(int skillId, String name, int chrId) {
        /* 1121 */
        MapleStatEffect effect = SkillFactory.getSkill(skillId).getEffect(getSkillLevel(skillId) + 1);
        /* 1122 */
        if (effect.getReqGuildLevel() > getLevel() || effect.getLevel() <= getSkillLevel(skillId)) {
            /* 1123 */
            return false;

        }
        /* 1125 */
        MapleGuildSkill ourSkill = this.guildSkills.get(Integer.valueOf(skillId));
        /* 1126 */
        if (ourSkill == null) {
            /* 1127 */
            ourSkill = new MapleGuildSkill(skillId, effect.getLevel(), 0L, name, name);
            /* 1128 */
            this.guildSkills.put(Integer.valueOf(skillId), ourSkill);

        } else {
            /* 1130 */
            ourSkill.level = effect.getLevel();
            /* 1131 */
            ourSkill.purchaser = name;
            /* 1132 */
            ourSkill.activator = name;

        }
        /* 1134 */
        if (effect.getPeriod() <= 0) {
            /* 1135 */
            ourSkill.timestamp = -1L;

        } else {
            /* 1137 */
            ourSkill.timestamp = System.currentTimeMillis() + effect.getPeriod() * 60000L;

        }
        /* 1139 */
        this.changed_skills = true;
        /* 1140 */
        gainGP(1000, true, chrId);
        /* 1141 */
        broadcast(GuildPacket.guildSkillPurchased(this.id, skillId, ourSkill.level, ourSkill.timestamp, name, name));
        /* 1142 */
        return true;

    }



    public int getLevel() {
        /* 1146 */
        return this.level;

    }



    public final int calculateLevel() {
        /* 1150 */
        for (int i = 1; i < 10; i++) {
            /* 1151 */
            if (this.gp < getGuildExpNeededForLevel(i)) {
                /* 1152 */
                return i;

            }

        }
        /* 1155 */
        return 10;

    }



    public int[] getGuildExp() {
        /* 1159 */
        return this.guildExp;

    }



    public int getGuildExpNeededForLevel(int levelx) {
        /* 1163 */
        if (levelx < 0 || levelx >= this.guildExp.length) {
            /* 1164 */
            return Integer.MAX_VALUE;

        }
        /* 1166 */
        return this.guildExp[levelx];

    }



    public void addMemberData(MaplePacketLittleEndianWriter mplew) {
        /* 1170 */
        List<MapleGuildCharacter> players = new ArrayList<>();
        /* 1171 */
        for (MapleGuildCharacter mgc : this.members) {
            /* 1172 */
            if (mgc.getId() == this.leader) {
                /* 1173 */
                players.add(mgc);

            }

        }
        /* 1176 */
        for (MapleGuildCharacter mgc : this.members) {
            /* 1177 */
            if (mgc.getId() != this.leader) {
                /* 1178 */
                players.add(mgc);

            }

        }
        /* 1181 */
        if (players.size() != this.members.size()) {
            /* 1182 */
            System.out.println("家族成员信息加载错误 - 实际加载: " + players.size() + " 应当加载: " + this.members.size());

        }

        /* 1185 */
        mplew.writeShort(players.size());
        /* 1186 */
        for (MapleGuildCharacter mgc : players) {
            /* 1187 */
            mplew.writeInt(mgc.getId());

        }
        /* 1189 */
        for (MapleGuildCharacter mgc : players) {
            /* 1190 */
            mplew.writeAsciiString(mgc.getName(), 13);
            /* 1191 */
            mplew.writeInt(mgc.getJobId());
            /* 1192 */
            mplew.writeInt(mgc.getLevel());
            /* 1193 */
            mplew.writeInt(mgc.getGuildRank());
            /* 1194 */
            mplew.writeInt(mgc.isOnline() ? 1 : 0);
            /* 1195 */
            mplew.writeInt(mgc.getAllianceRank());
            /* 1196 */
            mplew.writeInt(mgc.getGuildContribution());
            /* 1197 */
            mplew.writeInt(0);
            /* 1198 */
            mplew.writeInt(0);
            /* 1199 */
            mplew.writeLong(PacketHelper.getTime(-2L));

        }

        /* 1202 */
        mplew.writeShort(this.applyMembers.size());
        /* 1203 */
        for (MapleGuildCharacter mgc : this.applyMembers) {
            /* 1204 */
            mplew.writeInt(mgc.getId());

        }
        /* 1206 */
        for (MapleGuildCharacter mgc : this.applyMembers) {
            /* 1207 */
            mplew.writeAsciiString(mgc.getName(), 13);
            /* 1208 */
            mplew.writeInt(mgc.getJobId());
            /* 1209 */
            mplew.writeInt(mgc.getLevel());
            /* 1210 */
            mplew.writeInt(0);
            /* 1211 */
            mplew.writeInt(mgc.isOnline() ? 1 : 0);
            /* 1212 */
            mplew.writeInt(3);
            /* 1213 */
            mplew.writeInt(0);
            /* 1214 */
            mplew.writeInt(0);
            /* 1215 */
            mplew.writeInt(0);
            /* 1216 */
            mplew.writeLong(PacketHelper.getTime(-2L));

        }

    }



    public Collection<MapleGuildCharacter> getMembers() {
        /* 1221 */
        return Collections.unmodifiableCollection(this.members);

    }



    public boolean isInit() {
        /* 1225 */
        return this.init;

    }






    public List<MapleBBSThread> getBBS() {
        /* 1232 */
        List<MapleBBSThread> ret = new ArrayList<>(this.bbs.values());
        /* 1233 */
        ret.sort(new MapleBBSThread.ThreadComparator());
        /* 1234 */
        return ret;

    }






    public int addBBSThread(String title, String text, int icon, boolean bNotice, int posterID) {
        /* 1241 */
        int add = (this.bbs.get(Integer.valueOf(0)) == null) ? 1 : 0;
        /* 1242 */
        this.changed = true;
        /* 1243 */
        int ret = bNotice ? 0 : Math.max(1, this.bbs.size() + add);
        /* 1244 */
        this.bbs.put(Integer.valueOf(ret), new MapleBBSThread(ret, title, text, System.currentTimeMillis(), this.id, posterID, icon));
        /* 1245 */
        return ret;

    }






    public void editBBSThread(int localthreadid, String title, String text, int icon, int posterID, int guildRank) {
        /* 1252 */
        MapleBBSThread thread = this.bbs.get(Integer.valueOf(localthreadid));
        /* 1253 */
        if (thread != null && (thread.ownerID == posterID || guildRank <= 2)) {
            /* 1254 */
            this.changed = true;
            /* 1255 */
            thread.setTitle(title);
            /* 1256 */
            thread.setText(text);
            /* 1257 */
            thread.setIcon(icon);
            /* 1258 */
            thread.setTimestamp(System.currentTimeMillis());

        }

    }







    public void deleteBBSThread(int localthreadid, int posterID, int guildRank) {
        /* 1267 */
        MapleBBSThread thread = this.bbs.get(Integer.valueOf(localthreadid));
        /* 1268 */
        if (thread != null && (thread.ownerID == posterID || guildRank <= 2)) {
            /* 1269 */
            this.changed = true;
            /* 1270 */
            this.bbs.remove(Integer.valueOf(localthreadid));

        }

    }






    public void addBBSReply(int localthreadid, String text, int posterID) {
        /* 1278 */
        MapleBBSThread thread = this.bbs.get(Integer.valueOf(localthreadid));
        /* 1279 */
        if (thread != null) {
            /* 1280 */
            this.changed = true;
            /* 1281 */
            thread.replies.put(Integer.valueOf(thread.replies.size()), new MapleBBSReply(thread.replies.size(), posterID, text, System.currentTimeMillis()));

        }

    }






    public void deleteBBSReply(int localthreadid, int replyid, int posterID, int guildRank) {
        /* 1289 */
        MapleBBSThread thread = this.bbs.get(Integer.valueOf(localthreadid));
        /* 1290 */
        if (thread != null) {
            /* 1291 */
            MapleBBSReply reply = thread.replies.get(Integer.valueOf(replyid));
            /* 1292 */
            if (reply != null && (reply.ownerID == posterID || guildRank <= 2)) {
                /* 1293 */
                this.changed = true;
                /* 1294 */
                thread.replies.remove(Integer.valueOf(replyid));

            }

        }

    }



    public boolean hasSkill(int id) {
        /* 1300 */
        return this.guildSkills.containsKey(Integer.valueOf(id));

    }


       private enum BCOp
             {
        /* 1305 */     NONE, DISBAND, EMBELMCHANGE, CHAT;

    }

}


/* Location:              C:\PGX\Server.jar!\handling\world\guild\MapleGuild.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */